<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */


class RedCross_Validate_ValidDate extends Zend_Validate_Abstract
{
    const ERROR = 'error';
    const NOT_VALID = 'notValid';
    
    const EARLIER_THAN_NOW = 'earlierThanNow';
    const TO_DATE_NOT_SET = 'toDateNotSet';
    const FROM_DATE_NOT_SET = 'fromDateNotSet';

    const FROM_DATE = 'from_date';
    const TO_DATE = 'to_date';

    protected $_messageTemplates = array(
        self::ERROR  =>  'En feil har oppstått',
        self::NOT_VALID  =>  'Datoen er ikke gyldig',
        self::EARLIER_THAN_NOW => 'Datoen har allerede passert',
        self::TO_DATE_NOT_SET => 'Sluttdato er ikke satt',
        self::FROM_DATE_NOT_SET => 'Startdato er ikke satt',
    );

    private $fieldName;
    private $earlierThanNowAllowed = false;

    public function __construct(array $options = null)
    {
        if ($options != null && $options['type'] === self::FROM_DATE) {
            $this->fieldName = self::FROM_DATE;
        } else if ($options != null && $options['type'] === self::TO_DATE) {
            $this->fieldName = self::TO_DATE;
        }
        if ($options != null && isset($options['earlier']) && $options['earlier'] === true) {
            $this->earlierThanNowAllowed = true;
        }
    }

    public function isValid($value, $context = null)
    {
        $value = (string) $value;
        $this->_setValue($value);

        $now = new RedCross_Date();
        $date = new RedCross_Date($value, RedCross_Date::NO_DATE_FMT);

        if (is_array($context)) {

        	if ($this->fieldName === self::FROM_DATE && isset($context['to_date'])) {

                if (!$this->earlierThanNowAllowed && ($now->isLater($date) && !$date->isToday())) {
                    $this->_error(self::EARLIER_THAN_NOW);
                    return false;
                }
                
                if (!RedCross_Date::isDate($context['to_date'], RedCross_Date::NO_DATE_FMT)) {
                    return true;
                }

                $compare = new RedCross_Date($context['to_date'], RedCross_Date::NO_DATE_FMT);
        		if ($date->isEarlier($compare) || $date->compareDate($compare) == 0) {
                    return true;
                }

        	} else if ($this->fieldName === self::TO_DATE && isset($context['from_date'])) {

                if (!$this->earlierThanNowAllowed && ($now->isLater($date) && !$date->isToday())) {
                    $this->_error(self::EARLIER_THAN_NOW);
                    return false;
                }

                if (!RedCross_Date::isDate($context['from_date'], RedCross_Date::NO_DATE_FMT)) {
                    return true;
                }

                $compare = new RedCross_Date($context['from_date'], RedCross_Date::NO_DATE_FMT);
        		if ($date->isLater($compare)  || $date->compareDate($compare) == 0) {
                    return true;
                }
                
            } else {
                $this->_error(self::ERROR);
                return false;
            }
        }
        $this->_error(self::NOT_VALID);
        return false;
    }
}